package com.mf.ergate.controller.auth;

import com.mf.ergate.api.auth.ResourceInfoRemoteService;
import com.mf.ergate.api.auth.model.dto.ResourceInfoCreateDto;
import com.mf.ergate.api.auth.model.dto.ResourceInfoDeleteDto;
import com.mf.ergate.api.auth.model.dto.ResourceInfoQueryDto;
import com.mf.ergate.api.auth.model.dto.ResourceInfoUpdateDto;
import com.mf.ergate.api.auth.model.vo.ResourceInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.auth.ResourceInfoEntity;
import com.mf.ergate.service.auth.ResourceInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 资源信息Controller
 * User: zhaoming
 * DateTime: 2018-10-10
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class ResourceInfoController implements ResourceInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ResourceInfoService resourceInfoService;

    /**
     * 新增
     * @param resourceInfoCreateDto 资源信息新增Dto
     * @return 资源信息
     */
    public Result<ResourceInfoVo> insert(@RequestBody ResourceInfoCreateDto resourceInfoCreateDto) {
        ResourceInfoEntity resourceInfoEntity = MfBeanUtils.copyObject(resourceInfoCreateDto, ResourceInfoEntity.class);
        resourceInfoEntity = resourceInfoService.insert(resourceInfoEntity);
        ResourceInfoVo resourceInfoVo = MfBeanUtils.copyObject(resourceInfoEntity, ResourceInfoVo.class);
        return new Result<>(resourceInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param resourceInfoUpdateDto 资源信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody ResourceInfoUpdateDto resourceInfoUpdateDto) {
        ResourceInfoEntity resourceInfoEntity = MfBeanUtils.copyObject(resourceInfoUpdateDto, ResourceInfoEntity.class);
        Boolean flag = resourceInfoService.updateByUniqueNo(resourceInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param resourceInfoDeleteDto 资源信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody ResourceInfoDeleteDto resourceInfoDeleteDto){
        ResourceInfoEntity resourceInfoEntity = MfBeanUtils.copyObject(resourceInfoDeleteDto, ResourceInfoEntity.class);
        Boolean flag = resourceInfoService.deleteByUniqueNo(resourceInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param resourceNo 资源编号
     * @return 资源信息
     */
    public Result<ResourceInfoVo> selectByUniqueNo(@RequestBody String resourceNo) {
        ResourceInfoEntity resourceInfoEntity = resourceInfoService.selectByUniqueNo(resourceNo);
        ResourceInfoVo resourceInfoVo = MfBeanUtils.copyObject(resourceInfoEntity, ResourceInfoVo.class);
        return new Result<>(resourceInfoVo);
    }

    /**
     * 根据条件查询
     * @param resourceInfoQueryDto 资源信息查询Dto
     * @return 资源信息列表
     */
    public Result<List<ResourceInfoVo>> selectByCondition(@RequestBody ResourceInfoQueryDto resourceInfoQueryDto) {
        List<ResourceInfoEntity> resourceInfoEntities = resourceInfoService.selectByCondition(resourceInfoQueryDto);
        List<ResourceInfoVo> resourceInfoVos = MfBeanUtils.copyList(resourceInfoEntities, ResourceInfoVo.class);
        return new Result<>(resourceInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param resourceInfoQueryDto 资源信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody ResourceInfoQueryDto resourceInfoQueryDto) {
        return new Result<>(resourceInfoService.selectCount(resourceInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 资源信息分页列表
     */
    public Result<Page<ResourceInfoVo>> selectPage(@RequestBody RemotePage<ResourceInfoQueryDto> remotePage){
        Page<ResourceInfoEntity> resourceInfoEntityPage = resourceInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<ResourceInfoVo> page = MfBeanUtils.copyPage(resourceInfoEntityPage, ResourceInfoVo.class);
        return new Result<>(page);
    }

    /**
     * 根据应用类型编码与用户编号查询
     * @param appTypeCode 应用类型编码
     * @param userNo 用户编号
     * @return 资源信息列表
     */
    public Result<List<ResourceInfoVo>> selectByAppTypeCodeAndUserNo(@RequestParam("appTypeCode") String appTypeCode, @RequestParam("userNo") String userNo){
        List<ResourceInfoEntity> resourceInfoEntities = resourceInfoService.selectByAppTypeCodeAndUserNo(appTypeCode, userNo);
        List<ResourceInfoVo> resourceInfoVos = MfBeanUtils.copyList(resourceInfoEntities, ResourceInfoVo.class);
        return new Result<>(resourceInfoVos);
    }

    /**
     * 删除资源及相关信息(逻辑删除)
     * @param resourceInfoDeleteDto 资源信息删除Dto
     * @return 执行结果
     */
    public Result<Boolean> deleteCompleteByUniqueNo(@RequestBody ResourceInfoDeleteDto resourceInfoDeleteDto){
        ResourceInfoEntity resourceInfoEntity = MfBeanUtils.copyObject(resourceInfoDeleteDto, ResourceInfoEntity.class);
        Boolean flag = resourceInfoService.deleteCompleteByUniqueNo(resourceInfoEntity);
        return new Result<>(flag);
    }
}